<template>
  <div class="kind">
    <el-row :gutter="4" style="margin:10px 0px">
      <el-col :span="2"><el-button size="small" type="primary" @click="add">新建分类</el-button> </el-col>
    </el-row>
    <el-row>
      <el-table ref="ex" :data="noticeData" style="width: 100%" border>
        <el-table-column type="index" label="序号" width="50" align="center" :index="indexSort"></el-table-column>
        <TableTreeColumn prop="typeName" treeKey="shTypeId" levelKey="_level" label="分类名称">
        </TableTreeColumn>
        <!-- <el-table-column prop="typeName" label="分类名称" align="left"></el-table-column> -->
        <el-table-column prop="shType.typeName" label="修改时间" align="center"></el-table-column>
        <el-table-column prop="" label="操作" align="center" width="200">
          <template slot-scope="scope">
            <span
              class="el-tag el-tag--warning el-tag--small"
              style="cursor: pointer;"
              @click="modify(scope.row)"
              size="mini"
            >修改</span>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row style="text-align: right;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[15, 50, 100, 200]"
        :page-size="pageSize"
        layout="->, sizes, prev, pager, next, jumper, total"
        :total="totalElements"
      ></el-pagination>
    </el-row>
    <addOrEditKind :off.sync="supplierDialog" :rowData="rowdata" :title="title"></addOrEditKind>
  </div>
</template>

<script>
import { getKindList,delWarehouse } from "@/api/jxc/warehouse.js";
import * as util from "@/util/util.js";
export default {
  name: "kind",
  components: {
      "addOrEditKind": () => import("./addOrEditKind"),
      "TableTreeColumn": () => import("@/components/table-tree-column"),
  },
  data() {
    return {
      goodsNumber: "",
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      selectDate: "",
      noticeData: [],
      pageSize: 15,
      currentPage: 1,
      totalElements: 0,
      totalPages: 1,
      supplierDialog: false,
      rowdata: {},
      title: "",
      index: 1,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // expand(row,rowAndExpand){
    //     this.$refs.ex.doLayout();
    //     console.log(row, rowAndExpand);
    // },
    stop(row){

    },
    open(row){},
    modify(row){
      console.log(row);
      this.title = "修改分类";
      this.rowdata = row;
      this.supplierDialog = true;
    },
    add() {
      this.title = "新增分类";
      this.supplierDialog = true;
    },
    del(row) {
      console.log("row", row);
      delWarehouse({ storehouseId: row.storehouseId }).then(
        res => {
          this.$notify.success("删除成功");
          this.getList();
        },
        err => {}
      );
    },
    getList() {
      getKindList({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      }).then(
        res => {
          console.log("res仓库分类接口", res);
          this.noticeData = res.data;
          this.setLevels(this.noticeData, 0);
          console.log("this.noticeData", this.noticeData);
          this.noticeData.forEach(e => {
            e.createDate = util.timestampToTime(e.createDate);
          });
          this.totalElements = res.data.total;
        },
        err => {}
      );
    },
    //设置级别
    setLevelForItem(arr){
        arr.forEach((e, i) => {
            e._level = this.index++;
            // e.index = i;
            if(e.list.length > 0){
                this.setLevelForItem(e.list);
            }
        })
        return arr;
    },
    setLevels(childNodeList, curLevel) {
        for (var i = 0; i < childNodeList.length; i++) {
          childNodeList[i]._level = curLevel + 1
          if (childNodeList[i].list && childNodeList[i].list.length > 0) {
            this.setLevels(childNodeList[i].list, childNodeList[i]._level);
          }
        }
      },
    load(tree, treeNode, resolve) {
        setTimeout(() => {
          resolve([
            
          ])
        }, 1000)
    },
    //序号
    indexSort(index) {
        // console.log("index", index);

    //   if (this.currentPage > 1) {
    //     index = (this.currentPage - 1) * this.pageSize + index + 1;
    //   } else {
    //     index = index + 1;
    //   }
        
      return ++index;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
      console.log(`当前页: ${val}`);
    }
  }
};
</script>

<style lang="less" props>
.pushList {
}
</style>